﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;

namespace UIFramework.Editor
{
    public class OverrideUIMenu
    {
        /*
         重写UI组件创建菜单
             */

        [MenuItem("GameObject/UI/Button")]
        static void CreatUIButton()
        {
            Transform parent = Selection.activeTransform;
            GameObject btn = new GameObject("UIButton");
            ResetPosAndParent(btn, parent);
            btn.AddComponent<Image>();
            btn.AddComponent<UIButton>();
            GameObject lbl = new GameObject("Text");
            Text txt = lbl.AddComponent<Text>();
            txt.text = "Button";
            txt.alignment = TextAnchor.MiddleCenter;
            txt.color = Color.black;
            txt.raycastTarget = false;
            ResetPosAndParent(lbl, btn.transform);
        }

        [MenuItem("GameObject/UI/Button Empty")]
        static void CreatEmptyButton()
        {
            Transform parent = Selection.activeTransform;
            GameObject btn = new GameObject("UIButton");
            ResetPosAndParent(btn, parent);
            btn.AddComponent<Empty4Raycast>();
            btn.AddComponent<UIButton>().transition = Selectable.Transition.None;
            GameObject lbl = new GameObject("Text");
            Text txt = lbl.AddComponent<Text>();
            txt.text = "Button";
            txt.alignment = TextAnchor.MiddleCenter;
            txt.color = Color.black;
            txt.raycastTarget = false;
            ResetPosAndParent(lbl, btn.transform);
        }

        [MenuItem("GameObject/UI/Image")]
        static void CreatImage()
        {
            Transform parent = Selection.activeTransform;
            GameObject go = new GameObject("Image", typeof(Image));
            go.GetComponent<Image>().raycastTarget = false;
            ResetPosAndParent(go, parent);
        }

        [MenuItem("GameObject/UI/Image-FullScreen")]
        static void CreatFullScreenImage()
        {
            Transform parent = Selection.activeTransform;
            GameObject go = new GameObject("Image", typeof(Image));
            go.GetComponent<Image>().raycastTarget = true;
            ResetPosAndParent(go, parent);
            RectTransform rt = go.GetComponent<RectTransform>();
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.one;
            rt.sizeDelta = Vector2.zero;
            go.transform.SetAsFirstSibling();
        }

        [MenuItem("GameObject/UI/Text")]
        static void CreatText()
        {
            Transform parent = Selection.activeTransform;
            GameObject go = new GameObject("Text", typeof(Text));
            go.GetComponent<Text>().raycastTarget = false;
            ResetPosAndParent(go, parent);
        }

        [MenuItem("GameObject/UI/UIText/方便颜色切换的文本")]
        static void CreatUIText()
        {
            Transform parent = Selection.activeTransform;
            GameObject go = new GameObject("UIText", typeof(UIText));
            go.GetComponent<UIText>().raycastTarget = false;
            ResetPosAndParent(go, parent);
            go.tag = "SpawnNuclearUI";
        }

        [MenuItem("GameObject/UI/Raw Image")]
        static void CreatRawImage()
        {
            Transform parent = Selection.activeTransform;
            GameObject go = new GameObject("RawImage", typeof(RawImage));
            go.GetComponent<RawImage>().raycastTarget = false;
            ResetPosAndParent(go, parent);
        }

        static void ResetPosAndParent(GameObject go, Transform parent)
        {
            if (parent != null)
            {
                go.transform.SetParent(parent);
                go.layer = parent.gameObject.layer;
            }
            go.transform.localPosition = Vector3.zero;
            go.transform.localRotation = Quaternion.identity;
            go.transform.localScale = Vector3.one;
        }

    }
}
